package com.qiwx.pigeon.activity.account;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.Utils.SharedPreferencesUtil;
import com.qiwx.pigeon.Utils.StringUtil;
import com.qiwx.pigeon.base.BaseMVPActivity;
import com.qiwx.pigeon.data.params.UpdatePwdParam;
import com.qiwx.pigeon.data.vo.EmployBean;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.presenter.UserManagerPresenter;
import com.qiwx.pigeon.viewinterface.IUserManagerView;
import com.qiwx.pigeon.widget.ArrowView;

import java.lang.ref.WeakReference;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @description
 * @Author qiwx
 * @Date 2020-06-06 21:31
 * 修改密码
 **/
public class ModifyPwdActivity extends BaseMVPActivity<IUserManagerView, UserManagerPresenter> implements IUserManagerView {
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_return)
    ArrowView titleReturn;
    @BindView(R.id.edt_new_pwd)
    EditText edtNewPwd;
    @BindView(R.id.edt_new_pwd2)
    EditText edtNewPwd2;
    @BindView(R.id.btn_ok)
    Button btnOk;
    EmployBean employBean;

    @Override
    public UserManagerPresenter createPresenter() {
        return new UserManagerPresenter(new WeakReference(this));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_modify_pwd;
    }

    @Override
    public void initAfterOnCreate() {
        employBean = SharedPreferencesUtil.getEmployBean(this);
        if (employBean == null) {
            MyToast.showShort("重新登录");
            finish();
        }
        initView();
    }

    private void initView() {
        titleReturn.changeArrowLeft();
        titleContent.setText("修改密码");
    }

    private boolean checkParam() {
        String pwd1=edtNewPwd.getText().toString();
        String pwd2=edtNewPwd2.getText().toString();
        if(StringUtil.isEmpty(pwd1)){
            MyToast.showShort("新密码不能为空");
            return false;
        }
        if(StringUtil.isEmpty(pwd2)){
            MyToast.showShort("确认密码不能为空");
            return false;
        }
        if(!pwd1.equals(pwd2)){
            MyToast.showShort("两次输入密码不相同");
            return false;
        }
        return true;

    }


    @Override
    public void onMemberSuccess(PageInfo<EmployBean> datas) {

    }

    @Override
    public void onSuccess(int code) {
        MyToast.showShort("修改密码成功");
        SharedPreferencesUtil.setLoginInfo(this, employBean.getLogin_name(), edtNewPwd.getText().toString());
        finish();
    }

    @Override
    public void onFailure(String error) {

    }


    @OnClick({R.id.title_return, R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_return:
                finish();
                break;
            case R.id.btn_ok:
                if (checkParam()) {
                    UpdatePwdParam param = new UpdatePwdParam();
                    param.setId(employBean.getId());
                    param.setPwd(edtNewPwd.getText().toString());
                    mPresenter.updatePwd(param);
                }

                break;
        }
    }
}
